package ie.dit.fyp;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import net.htmlparser.jericho.Attribute;
import net.htmlparser.jericho.Attributes;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import net.htmlparser.jericho.TextExtractor;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetReview {
	public static void main(String[] args) {
		HttpClient client = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(
				"http://amazon.com/o/tg/detail/-/1451648537/?vi=customer-reviews");
		HttpResponse response;
		List<String> ratings = new ArrayList<String>();
		List<String> reviews = new ArrayList<String>();

		try {
			response = client.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream is = entity.getContent();
				Source source = new Source(is);
				List<Element> elementList = source.getAllElements();
				for (Element e : elementList) {
					e.toString();
					List<StartTag> tags = e.getAllStartTags();
					for (StartTag tag : tags) {
						if (tag.getName().equals("span")) {
							Attributes atts = tag.getAttributes();
							Attribute startClass = atts.get("class");
							if (startClass != null) {
								String rating = startClass.getValue();
								if (rating.contains("swSprite s_star")) {
									// Rating tag
									ratings.add(rating.substring(0, 3));
								}

							}
						} else if (tag.getName().equals("div")) {
							Attributes atts = tag.getAttributes();
							Attribute style = atts.get("style");
							if (style != null
									&& style.getValue().equals(
											"margin-left:0.5em;")) {
								Element ele = tag.getElement();
								TextExtractor extractor = new TextExtractor(
										ele.getContent()) {
									@Override
									public boolean excludeElement(
											StartTag startTag) {
										return startTag.getName().equals("div")
												|| startTag.getName().equals(
														"a");
									}
								};

								reviews.add(extractor.toString());
							}
						}
					}
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
